package com.cr.admin.business.banner.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.admin.business.banner.mapper.BannerMapper;
import com.cr.admin.business.banner.pojo.entity.Banner;
import com.cr.admin.business.banner.pojo.param.BannerAddParam;
import com.cr.admin.business.banner.pojo.param.BannerUpdateParam;
import com.cr.admin.business.banner.pojo.query.BannerQuery;
import com.cr.admin.business.banner.service.BannerService;
import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Override
    public List<Banner> list(BannerQuery bannerQuery) {
        QueryWrapper<Banner> queryWrapper= new QueryWrapper<>();
        if(bannerQuery.getId()!=null){
            queryWrapper.eq("id",bannerQuery.getId());
        }
        if(bannerQuery.getDescription()!=null && !bannerQuery.getDescription().isEmpty()){
            queryWrapper.like("description",bannerQuery.getDescription());
        }
        return bannerMapper.selectList(queryWrapper);
    }

    @Override
    public void insert(BannerAddParam bannerAddParam) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerAddParam,banner);
        bannerMapper.insert(banner);
    }

    @Override
    public void updateById(BannerUpdateParam bannerUpdateParam) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerUpdateParam,banner);
        bannerMapper.updateById(banner);
    }

    @Override
    public Banner selectById(Long id) {
        return bannerMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        bannerMapper.deleteBatchIds(ids);
    }
}
